
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';


import { News } from '../models/News';

@Injectable()
export class NewsService {

    private baseUrl =  'api/news';

    constructor(
        private http: HttpClient
    ) {
    }



    getNewses(): Observable<News[]> {
        return this.http.get<News[]>(this.baseUrl);
    }



    getNews(id: string): Observable<News> {
        return this.http.get<News>(`${this.baseUrl}/${id}`);
    }



    update(news: News): Observable<News> {
        const headers = new HttpHeaders({'Content-Type': 'application/json'});
        const url = `${this.baseUrl}/${news.id}`;
        return this.http
            .put<News>(url, JSON.stringify(news), {headers: headers});
    }

    create(name: string): Observable<News> {
        const headers = new HttpHeaders({'Content-Type': 'application/json'});
        return this.http
            .post<News>(this.baseUrl, JSON.stringify({name: name}), {headers: headers});
    }

    del(id: number): Observable<any> {
        const url = `${this.baseUrl}/${id}`;
        const headers = new HttpHeaders({'Content-Type': 'application/json'});
        return this.http.delete(url, {headers: headers});
    }




}
